﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Microsoft.Xna.Framework;

namespace Campus {
    class BuildUnitAbility : Ability {

        private int m_elapsedTime;
        public int ElapsedTime {
            get { return m_elapsedTime; }
        }

        private BuildRequirements.Units m_unitType;
        public BuildRequirements.Units UnitType
        {
            get { return m_unitType; }
        }

        public BuildUnitAbility(AbilityMenuIcon menuIcon, Structure owner, BuildRequirements.Units unit)
            : base(menuIcon, owner, null, TargetTypes.None) {
            m_unitType = unit;
            m_elapsedTime = 0;
            MenuDelegate = delegate(string option) {
                BuildRequirements.Requirements reqs = BuildRequirements.GetRequirements(m_unitType);
                if (reqs.CheckRequirements(Owner.PlayerOwner) == true) {
                    Owner.SetCurrentAbility(this);
                } else {
                    Type unitType = BuildRequirements.Requirements.GetType(m_unitType);
                    string name = unitType.Name;
                    Owner.PlayerOwner.CreateHUDMessage("Construction aborted for " + name + ".  You do not have sufficient resources", 10);
                }
            };
        }
        public BuildUnitAbility(BuildUnitAbility copySource)
            : base(copySource) {
            m_unitType = copySource.m_unitType;
            m_elapsedTime = copySource.m_elapsedTime;
            MenuDelegate = delegate(string option) {
                BuildRequirements.Requirements reqs = BuildRequirements.GetRequirements(m_unitType);
                if (reqs.CheckRequirements(Owner.PlayerOwner) == true) {
                    Owner.SetCurrentAbility(this);
                } else {
                    Type unitType = BuildRequirements.Requirements.GetType(m_unitType);
                    string name = unitType.Name;
                    Owner.PlayerOwner.CreateHUDMessage("Construction aborted for " + name + ".  You do not have sufficient resources", 10);
                }
            };
        }
        public override Ability Clone() {
            return new BuildUnitAbility(this);
        }
        public override void Update(GameTime gameTime, bool checkInput, bool updateBoundingBox) {
            m_elapsedTime += gameTime.ElapsedRealTime.Milliseconds;
            BuildRequirements.Requirements reqs = BuildRequirements.GetRequirements(m_unitType);
            //TODO: add check at beginning - do in button delegate
            if (reqs.BuildTime <= m_elapsedTime) {
                if (reqs.CheckRequirements(Owner.PlayerOwner) == true) {
                    Unit unit = BuildRequirements.BuildUnit(m_unitType, Owner.PlayerOwner);
                    Player p = Owner.PlayerOwner;
                    p.AddMoney(-reqs.MoneyCost);
                    p.AddResearch(-reqs.ResearchCost);
                    p.AddUsedFood(reqs.FoodCost);
                    Vector2 gridPos = Cursor.GetTileAtPoint(Owner.Position, Owner.PlayerOwner);
                    Owner.PlayerOwner.CurrentLevel.AddBaseEntityToTile(unit, (int)gridPos.X, (int)gridPos.Y);
                    unit.Stop();
                    IsAbilityDone = true;
                } else {
                    Type unitType = BuildRequirements.Requirements.GetType(m_unitType);
                    string name = unitType.Name;
                    Owner.PlayerOwner.CreateHUDMessage("Construction aborted for " + name + ".  You do not have sufficient resources", 10);
                    IsAbilityDone = true;
                }
            }
        }
    }
}
